/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.editpart;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.figures.ClassifierFigure;
import com.cea.papyrus.core.figures.ContainerFigure;
import com.cea.papyrus.core.figures.EnumerationFigure;
import com.cea.papyrus.core.figures.IconFigure;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * An editPart used to show a list of features of a class.
 * 
 * @author dumoulin
 */
public class FeatureListEditPart extends ContainerEditPart
		implements PropertyChangeListener {

	/**
	 * The element type accepted by the associated list model.
	 * If null, any type is accepted.
	 */
	protected Class elementType;
	
	/**
	 * 
	 */
	protected int channel=14;
	
	/**
	 * Create a FeatureList EditPart accepting any kind from drag and drop.
	 * 
	 * @param modelManager 
	 * @param graphnode 
	 */

	public FeatureListEditPart(GraphNode graphnode, ModelManager modelManager) {
		super(graphnode,modelManager);
		Debug.debug(this,"create ClassFeatureListEditPart()",channel);
		this.elementType = Object.class;
	}

	/**
	 * Create a FeatureList EditPart accepting specified type from drag and drop.
	 * Create a ListEditPart for a List accepting only specified element.
	 * 
	 * @param modelManager 
	 * @param elementType 
	 * @param graphnode 
	 */
	public FeatureListEditPart(GraphNode graphnode,Class elementType,ModelManager modelManager) {
		super(graphnode,modelManager);
		Debug.debug(this,"create ClassFeatureListEditPart("+ elementType+")",channel);
		this.elementType = elementType;
	}

	/*
	 * Add editing capabilities
	 */
	/**
	 * 
	 */
	@Override
	protected void createEditPolicies() {
		// handles constraint changes (e.g. moving and/or resizing) of model
		// elements
		// and creation of new model elements
		
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new FeatureListFlowLayoutEditPolicy(elementType));
	}

	/**
	 * Upon activation, attach to the model element as a property change
	 * listener.
	 */
	@Override
	public void activate() {
		if (!isActive()) {
			super.activate();
			/*if (getModelList() instanceof EList)
				((EList) getModelList())
						.addPropertyChangeListener(this);*/
		}
	}

	/**
	 * Upon deactivation, detach from the model element as a property change
	 * listener.
	 */
	@Override
	public void deactivate() {
		if (isActive()) {
			super.deactivate();}
		/*	if (getModelList() instanceof Elist)
				((EList) getModelList())
						.removePropertyChangeListener(this);
		}*/
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
	 */
	/**
	 * 
	 * 
	 * @param evt 
	 */
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		// String prop = evt.getPropertyName();
		refreshChildren();
		refreshVisuals();
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	protected IFigure createFigure() {
		return new ContainerFigure();
	}
		
	/**
	 * 
	 */
	@Override
	protected void refreshVisuals() {
		
		ContainerFigure f = (ContainerFigure) getFigure();
		f.setBackgroundColor(PapyrusPlugin.colorManager.get(getGraphNode().getContainer().getBackgroundColor()));
		
		IFigure parentFigure = ((GraphicalEditPart) getParent()).getFigure();
		
		// Shape presentation
		if (parentFigure instanceof IconFigure) {
			return;
		}
		
		/*if( elementType==org.eclipse.uml2.uml.TemplateParameter.class){
			if (((TemplateSignatureFigure)parentFigure).getTemplateParameterContent() == null)
				((TemplateSignatureFigure)parentFigure).addtemplateParameterContainer(f) ;
		}*/
		
		if (elementType==org.eclipse.uml2.uml.Property.class) {
			if( parentFigure instanceof ClassifierFigure){
				((ClassifierFigure) parentFigure).addpropertyContainer(f);}
			else{
				((EnumerationFigure) parentFigure).addFeatureContainer(f);
			}
			
		}
		
		if (elementType==org.eclipse.uml2.uml.Operation.class) {
			((ClassifierFigure) parentFigure).addmethodContainer(f);
		}
		
		if (elementType==org.eclipse.uml2.uml.EnumerationLiteral.class) {
			((EnumerationFigure) parentFigure).addFeatureContainer(f);
		}		
	}
}